package com.woniuxy.lab.woniu_lab_boss.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.lab.woniu_lab_boss.common.Result;
import com.woniuxy.lab.woniu_lab_boss.exception.WebException;
import com.woniuxy.lab.woniu_lab_boss.exception.WebExceptionCode;
import com.woniuxy.lab.woniu_lab_boss.model.Teacher;
import com.woniuxy.lab.woniu_lab_boss.model.User;
import com.woniuxy.lab.woniu_lab_boss.dao.UserDao;
import com.woniuxy.lab.woniu_lab_boss.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.lab.woniu_lab_boss.service.dto.TeacherDto;
import com.woniuxy.lab.woniu_lab_boss.service.dto.UserDto;
import com.woniuxy.lab.woniu_lab_boss.util.OSSUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author Silas
 * @since 2023-03-15
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserDao, User> implements UserService {
    @Resource
    private UserDao userDao;

    @Override
    public IPage<UserDto> search(String condition, Integer page, Integer size) {

        IPage<User> iPage = new Page<>(page == null ? 1 : page, size == null ? 4 : size);
        IPage<User> page1 = userDao.selectPage(iPage, new QueryWrapper<User>()
                .eq("phone", condition)
                .or()
                .eq("nickname", condition)
        );
        return page1.convert(u -> {
            UserDto userDto = new UserDto();
            BeanUtils.copyProperties(u, userDto);
            return userDto;
        });
    }

    @Override
    public IPage<UserDto> findAll(Integer page, Integer size) {

        IPage<User> iPage = new Page<>(page == null ? 1 : page, size == null ? 4 : size);
        IPage<User> page1 = userDao.selectPage(iPage, new QueryWrapper<User>());
        return page1.convert(u -> {
            UserDto userDto = new UserDto();
            BeanUtils.copyProperties(u, userDto);
            return userDto;
        });
    }

    @Override
    public IPage<UserDto> findVIP(String vip,Integer page, Integer size) {
        IPage<User> iPage = new Page<>(page == null ? 1 : page, size == null ? 4 : size);
        IPage<User> page1 = userDao.selectPage(iPage, new QueryWrapper<User>()
                .eq("vip", vip)
        );
        return page1.convert(u -> {
            UserDto userDto = new UserDto();
            BeanUtils.copyProperties(u, userDto);
            return userDto;
        });
    }

    @Override
    public IPage<UserDto> searchByState(String condition, Integer page, Integer size) {
        IPage<User> iPage = new Page<>(page == null ? 1 : page, size == null ? 4 : size);
        IPage<User> page1 = userDao.selectPage(iPage, new QueryWrapper<User>()
                .eq("status", condition));
        return page1.convert(u -> {
            UserDto userDto = new UserDto();
            BeanUtils.copyProperties(u, userDto);
            return userDto;
        });
    }

    @Override
    public String uploadFile(MultipartFile file) throws IOException {
        String originalFilename = file.getOriginalFilename();
        String key =System.currentTimeMillis() + originalFilename.substring(originalFilename.lastIndexOf("."));
        String s = OSSUtil.uploadFile(file.getInputStream(), key, "woniulap");
        return s;
    }


    @Override
    public void modifyStatus(Integer userId) {
        User user = getById(userId);
        if (user == null) throw new WebException(WebExceptionCode.USER_IS_NOT_EXISTS);
        if (user.getStatus().equals("ENABLE")) {
            user.setStatus("DISABLE");
            updateById(user);

        } else if (user.getStatus().equals("DISABLE")) {
            user.setStatus("ENABLE");
            updateById(user);
        } else {
            throw new WebException(WebExceptionCode.STATUS_IS_WRONG);
        }
    }

    // 获取平台总收益
    @Override
    public BigDecimal getTotalIncome() {
        List<User> users = this.list();
        BigDecimal sum = new BigDecimal("0.00");
        for (User user : users)
            sum = sum.add(user.getTotalMoney());
        return sum;
    }
}
